package views.seeker;

import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.DBUtil;
import views.job.JobAddFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Objects;

/*ok*/
public class SeekerAddFrame extends JFrame {
    ImageIcon imageIcon = new ImageIcon("src/images/seeker.png");
    private static final Logger LOGGER = LoggerFactory.getLogger(JobAddFrame.class);
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jLseekeridentity = new JLabel("身份证号：");
    private JLabel jLseekername = new JLabel("姓名：");
    private JLabel jLseekergender = new JLabel("性别：");
    private JLabel jLseekerage = new JLabel("年龄：");
    private JLabel jLcontactphone = new JLabel("联系电话：");
    private JTextField jTseekeridentity = new JTextField();
    private JTextField jTseekername = new JTextField();
    private JComboBox<String> jCgender = new JComboBox<String>();
    private JTextField jTage = new JTextField();
    private JTextField jTcontactphone = new JTextField();
    private JButton btnadd = new JButton("添加");
    private JButton btnreset = new JButton("重置");
    private JLabel jLtitle = new JLabel();


    public SeekerAddFrame() {
        setTitle("添加求职者");
        setSize(400, 340);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        //todo:窗口大小可变
        setResizable(true); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        jLtitle.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jLtitle.setHorizontalAlignment(JLabel.CENTER);
        jLtitle.setForeground(new Color(255, 51, 51));
        jLtitle.setText("添  加  求  职  者");

        //TODO:位置
        jLseekeridentity.setSize(100, 80);
        jLseekeridentity.setLocation(50, 5);
        jLseekeridentity.setHorizontalAlignment(JLabel.RIGHT);
        jTseekeridentity.setSize(150, 25);
        jTseekeridentity.setLocation(160, 33);

        jLseekername.setSize(100, 80);
        jLseekername.setLocation(50, 40);
        jLseekername.setHorizontalAlignment(JLabel.RIGHT);
        jTseekername.setSize(150, 25);
        jTseekername.setLocation(160, 70);

        jLseekergender.setSize(100, 80);
        jLseekergender.setLocation(50, 80);
        jLseekergender.setHorizontalAlignment(JLabel.RIGHT);
        jCgender.setSize(150, 25);
        jCgender.setLocation(160, 110);
        jCgender.addItem("男");
        jCgender.addItem("女");

        jLseekerage.setSize(100, 80);
        jLseekerage.setLocation(50, 120);
        jLseekerage.setHorizontalAlignment(JLabel.RIGHT);
        jTage.setSize(150, 25);
        jTage.setLocation(160, 150);

        jLcontactphone.setSize(100, 80);
        jLcontactphone.setLocation(50, 160);
        jLcontactphone.setHorizontalAlignment(JLabel.RIGHT);
        jTcontactphone.setSize(150, 25);
        jTcontactphone.setLocation(160, 187);


        panBtn.add(btnadd);
        panBtn.add(btnreset);
        panLab.setLayout(null);

        // panLab.add(jLabel1);
        panLab.add(jLseekeridentity);
        panLab.add(jLseekername);
        panLab.add(jLseekergender);
        panLab.add(jLseekerage);
        panLab.add(jLcontactphone);

        panLab.add(jTseekeridentity);
        panLab.add(jTseekername);
        panLab.add(jCgender);
        panLab.add(jTage);
        panLab.add(jTcontactphone);

        add(jLtitle, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btnadd.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String seekeridentity = jTseekeridentity.getText().trim();
                String seekername = jTseekername.getText().trim();
                String gender = Objects.requireNonNull(jCgender.getSelectedItem()).toString().trim();
                int seekerage = Integer.parseInt(jTage.getText().trim());
                String contactphone = jTcontactphone.getText().trim();


                Statement stmt = DBUtil.getStatement();
                @Language("MySQL")
                String sql = "select identityNum from job_seeker where identityNum='"
                        + seekeridentity
                        + "';";

                @Language("MySQL")
                String sql2 = "insert into job_seeker(identityNum, name, gender, age, contact_phone) values("
                        + "'"
                        + seekeridentity
                        + "','"
                        + seekername
                        + "','"
                        + gender
                        + "',"
                        + seekerage
                        + ",'"
                        + contactphone
                        + "')";
                try {
                    ResultSet rs = stmt.executeQuery(sql);
                    if (!rs.next()) {
                        stmt.executeUpdate(sql2);
                        JOptionPane.showMessageDialog(null, "添加成功。");
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "该求职者已经存在或身份证号输入错误！");
                        jTseekeridentity.setText("");
                        jTseekername.setText("");
                        jCgender.setSelectedIndex(0);
                        jTage.setText("");
                        jTcontactphone.setText("");
                    }
                } catch (Exception e) {
                    LOGGER.info("错误信息：" + e.getMessage());
                }
            }
        });

        btnreset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jTseekeridentity.setText("");
                jTseekername.setText("");
                jCgender.setSelectedIndex(0);
                jTage.setText("");
                jTcontactphone.setText("");
            }
        });
    }

    public static void main(String[] args) {
        new SeekerAddFrame().setVisible(true);
    }
}
